package service

import (
	"context"
	"google.golang.org/grpc"
	"qld2devservice/proto"
	"qld2devservice/utils"
)

const (
	ADDRESS_ALIMSG_Gdt = "gdtalimsgservice:8439"
)

func BalanceAliMassageGdt(userId, balance int64, msg string) (string, *utils.ErrorCode) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(ADDRESS_ALIMSG_Gdt, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "BalanceWeiXinMassage:1 ,gRPC服务器连接失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewAliMsgClient(conn)
	res := proto.BalanceAliMassageRequest{UserId: userId, Balance: balance, Msg: msg}
	utils.MyPrint(utils.PRINT_INFO, "BalanceWeiXinMassage request", res)
	resp, err := client.BalanceAliMassage(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "BalanceWeiXinMassage:2 ,gRPC调用失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "BalanceWeiXinMassage response", utils.TransProtoToJson(resp))
	utils.MyPrint(utils.PRINT_INFO, "BalanceWeiXinMassage msg", resp.Msg)
	return resp.Msg, nil
}
func ElectriciyAliMassageGdt(userId int64, msg, title, outletSn string) (string, *utils.ErrorCode) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(ADDRESS_ALIMSG_Gdt, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "ElectriciyWeiXinMassage:1 ,gRPC服务器连接失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewAliMsgClient(conn)
	res := proto.ElectriciyAliMassageRequest{
		UserId:   userId,
		Msg:      msg,
		Title:    title,
		OutletSn: outletSn,
	}
	utils.MyPrint(utils.PRINT_INFO, "ElectriciyWeiXinMassage request", res)
	resp, err := client.ElectriciyAliMassage(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "ElectriciyWeiXinMassage:2 ,gRPC调用失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "ElectriciyWeiXinMassage response", utils.TransProtoToJson(resp))
	utils.MyPrint(utils.PRINT_INFO, "ElectriciyWeiXinMassage msg", resp.Msg)
	return resp.Msg, nil
}
